// tnxcore.js
/**
 * 基于原生JavaScript的扩展支持
 */

import util from './tnxcore-util';
import app from './tnxcore-app';
import fss from './tnxcore-fss';
import $Enum from './Enum';
import $Properties from './Properties';
import $SafePromise from './SafePromise';
import $Yaml from './Yaml';
import Menu from './tnxcore-menu';
import wechat from './tnxcore-wechat';
import './tnxcore.css';

export const Enum = $Enum;
export const Properties = $Properties;
export const SafePromise = $SafePromise;
export const Yaml = $Yaml;

export function build(name, create) {
    const prefix = name + '@';
    let result;
    if (window.tnx && window.tnx.$id && window.tnx.$id.startsWith(prefix)) {
        result = window.tnx;
    } else {
        result = create();
        result.$id = prefix + new Date().format('yyyyMMddHHmmss');
        window.tnx = result;
    }
    result.build = build;
    return result;
}

export default build('tnxcore', () => {
    return {
        libs: {},
        util: util,
        app: app,
        fss: fss,
        Enum: $Enum,
        Properties: $Properties,
        SafePromise: $SafePromise,
        Yaml: $Yaml,
        Menu: Menu,
        wechat: wechat,
        alert(message, title) {
            const content = title ? (title + ':\n' + message) : message;
            alert(content);
            return Promise.resolve();
        },
        success(message) {
            return this.alert(message, '成功');
        },
        error(message) {
            return this.alert(message, '错误');
        },
        confirm(message, title) {
            const yes = confirm(title + ':\n' + message);
            return Promise.resolve(yes);
        },
    };
});
